package com.wanmait.translate.init.service.impl;

import com.wanmait.translate.dto.TypeContent;
import com.wanmait.translate.init.service.InitAuditSerice;
import com.wanmait.translate.supplier.mapper.SupplierTravelMapper;
import com.wanmait.translate.utils.AuditEnum;
import com.wanmait.translate.utils.DateUtil;
import com.wanmait.translate.utils.RedisCacheMessage;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/*
 * @Author huweibin
 * @Description 在这里初始化所有审核数
 * @Date 23:21 2020/11/23
 **/
@Service
public class InitAuditSericeImpl implements InitAuditSerice {
    @Resource
    private SupplierTravelMapper supplierTravelMapper;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * @Author huweibin
     * @Description 初始化此月所有审核数量
     * @Date 9:14 2020/11/25
     **/
    public void initToDo(String date) throws ParseException {
        //最近的日期字符串
        List<String> strings = DateUtil.getMonthMoreDay(date);
        //循环
        strings.forEach((day) -> {
            try {
                this.initOneDayToDo( day);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });
    }

    public void initOneDayToDo(String day) throws ParseException {
        //获得审核的Cache
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //获得所有类型的模板
        List<TypeContent> allTypeContent = TypeContent.getAllTypeContent();
        for (TypeContent content : allTypeContent) {
            //循环所有状态
            for (Object stateObj : content.getStates()) {
                TypeContent.State state=(TypeContent.State)stateObj;
                //获得缓存key
                String auditedTravelKey = RedisCacheMessage.getAuditKey(content,  state.getStateId(), day);
                //如果已经有了就不加载
                if (valueOperations.get(auditedTravelKey) != null) {
                    return;
                }
                //获得今天此审核要做的
                TypeContent typeContent = oneDayTodo(day, content.getEnum());
                //获得当前要做的数量
                Integer stateCount = typeContent.getStateCount(state);
                //redis 保存状态数量
                valueOperations.set(auditedTravelKey, stateCount);
            }

        }
    }
    /*
     * @Author huweibin
     * @Description 根据审核类型和日期  数据库查询各状态数
     * @Date 9:13 2020/11/25
     **/
    public TypeContent oneDayTodo(String day,AuditEnum aduit) {
        //新建出差审核
        TypeContent audited = new TypeContent(AuditEnum.TRAVELED_AUDIT);
        switch (aduit){
            case TRAVELED_AUDIT:{
                aduit.getStates().keySet().forEach((state)->{
                    Integer auditedCount = supplierTravelMapper.getAuditedCount(day,state);
                    audited.addState(AuditEnum.getStateName(AuditEnum.TRAVELED_AUDIT, state), state, auditedCount);
                });
                    break;
            }
        }

        return audited;
    }

}
